﻿using System.AddIn.Contract;
using System.AddIn.Pipeline;
using System.IO;
using DCT.AddIn.AddInView;
using DCT.AddIn.Contract;

namespace DCT.AddIn.AddInSideAdapter
{
    [AddInAdapter]
    public class DCTExtendedFunctionViewToContractAdapter : ContractBase, IDCTExtendedFunctionContract
    {
        private DCTExtendedFunctionAddInView _view;

        public DCTExtendedFunctionViewToContractAdapter(DCT.AddIn.AddInView.DCTExtendedFunctionAddInView view)
        {
            _view = view;
        }

        public int VisualPosition
        {
            get { return _view.VisualPosition; }
        }

        public INativeHandleContract GetVisual()
        {
            return FrameworkElementAdapters.ViewToContractAdapter(_view.GetVisual());
        }

        public void UpdateData()
        {
            _view.UpdateData();
        }

        public void Initialize(IHostObjectContract hostObj)
        {
            _view.Initialize(new HostObjectContractToViewAddInAdapter(hostObj));
        }
    }

    public class HostObjectContractToViewAddInAdapter : HostObject
    {
        private IHostObjectContract _contract;
        private ContractHandle _handle;

        public HostObjectContractToViewAddInAdapter(IHostObjectContract contract)
        {
            this._contract = contract;
            this._handle = new ContractHandle(contract);
        }

        public override Stream GetData()
        {
            return _contract.GetData();
        }

        public override void ReportProgress(double progressPercent)
        {
            _contract.ReportProgress(progressPercent);
        }

        public override void ApplyChanges(Stream xmlChanges)
        {
            _contract.ApplyChanges(xmlChanges);
        }
    }
}
